
var app = getApp();
import * as network_utils from '../../../../http/NetworkUtils.js';
import constants from '../../../../common/Constants.js';
import * as umfToast from '../../../../utils/UmfToast';
const PageRouter = require('../../../../utils/PageRouter.js');
import TimeUtils from "../../../../utils/TimeUtils";

var pageNo = "1";
const PageSize = "10";

const PageTypes = {
   AGENTSELECTION: "agentSelection",
};
Page({

    /**
     * 页面的初始数据
     */
    data: {
        hasmore: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (!app.globalData.systemInfo.deviceHeight) {
            app.globalData.systemInfo.deviceHeight = wx.getSystemInfoSync().windowHeight;
        }

        this.setData({
            pageType: options.pageType,
            deviceHeight: app.globalData.systemInfo.deviceHeight
        })
    },

    //代理商名称选择
    cellClick: function (e) {
        PageRouter.navigateBack({
            delta: 1,
            data: {
                agentData: e.currentTarget.dataset.current
            }
        })
    },

    queryClick: function (e) {
        pageNo = "1";
        if (e) {
            this.setData({
                content: e.detail.value.sAgentName
            })
        }

        this._requestList();
    },

    loadMore: function () {
        this._requestList();
    },

    /**
     * 请求代理商列表
     * @private
     */
    _requestList() {
        let params = {
            content: this.data.content,
            pageNo: pageNo,
            pageSize: PageSize,
        };
        network_utils.buildRequest(constants.Interfaces.FUNC_SEARCH_AGENT, params, {
            doHttpSucess: (response) => {
                if (constants.RetCodes.SUCCESS === response.retCode) {
                    this._requestSuccess(response);
                    return true
                }
                return false
            }
        }).send();
    },

    _requestSuccess: function (response) {
        let hasMore = response["agentList"].length === parseInt(PageSize);

        if (this.data.pageType === PageTypes.AGENTSELECTION && pageNo === "1") {
            //选择代理商的场合，第一条为全部
            let selectAll = {
                agentName: "全部"
            };
            response["agentList"].unshift(selectAll);
        }

        let filterList = this.convertList(response["agentList"]);
        this.setData({
            agentList: pageNo === "1" ? filterList : this.data.agentList.concat(filterList),
            hasMore: hasMore
        });

        if (!hasMore) {
            umfToast.showToast("已加载完所有数据")
        } else {
            pageNo = (parseInt(pageNo) + 1).toString();
        }
    },

    convertList(agentList) {
        if (this.data.pageType === PageTypes.AGENTSELECTION) {
            return agentList;
        }

        let maper = (item)=> {
            item.agentRegTime = item.agentRegTime.split(' ')[0];
            return item;
        };
        return agentList.map(maper);
    }

});